package com.qf.web;

import com.alibaba.fastjson.JSON;
import com.qf.model.Card;
import com.qf.model.Massage;
import com.qf.model.User;
import com.qf.model.pojo.UserCard;
import com.qf.service.AccountService;
import com.qf.service.ProductService;
import com.qf.service.impl.AccountServiceImpl;
import com.qf.service.impl.ProductServiceImpl;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @program: bank-master
 * @description:
 * @author: mrsheng
 * @create: 2022-10-09 23:17
 **/
@SuppressWarnings("all")
@WebServlet("/BankServlet")
public class BankServlet extends BascServlet{
    AccountService accountService=new AccountServiceImpl();
    ProductService productService=new ProductServiceImpl();

    //查找银行卡号是否存在
  public void findIdCard(HttpServletRequest req, HttpServletResponse resp) throws Exception{
      String c_id = req.getParameter("c_id");
      UserCard userCard = accountService.findCardById(c_id);
      Massage<UserCard> m=null;
      if (userCard!=null){
          //成功
      m=new Massage<>(1,"成功",userCard);
      }else {

      m=new Massage<>(-1,"银行卡不存在");

      }
      String s = JSON.toJSONString(m);
      resp.getWriter().write(s);
  }

    //用户Id查找这个用户的所有银行卡
    public void findUid(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String u_id = req.getParameter("u_id");
        List<UserCard> list = accountService.findUid(u_id);
        Massage<List<UserCard>> m=null;
        if (list!=null){
            //存在
            m = new Massage<>(1, "成功",list);
        }else {
            //不存在
            m=new Massage<>(-1,"未找到该用户");
        }
        String s=JSON.toJSONString(m);
        resp.getWriter().write(s);

    }


    //转账
    //1.判断转入、转出账户是否存在
    //2.判断转入账户和转出账户是否过审
    //3.判断转出余额是否大于转账金额
    //4.判断验证码是否正确
    //5.判断支付密码是否正确
    //6.事务对转账的支持
    //7.判断转账是否成功
    //8.日志
    public void transfer(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String cid1 = req.getParameter("c_id1");
        String cid2 = req.getParameter("c_id2");
        Double transferMoney=Double.parseDouble(req.getParameter("fmoney"));
        String c_pwd = req.getParameter("pwd");//支付密码
        String code = req.getParameter("code");//验证码
        // 获取 Session 中的验证码
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        // 删除 Session 中的验证码
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        Massage<Card> m=null;
        //调用业务层判断这个账户是否存在
        Card card1 = accountService.findCardByCid(cid1); //转出
        Card card2 = accountService.findCardByCid(cid2); //转入
        if (card2!=null ){
            //账户存在,判断是否过审
            if (card1.getC_check()==1&&card2.getC_check()==1){
                //账户过审，判断余额是否大于转账金额
                if (card1.getC_balance()>=transferMoney){
                    //余额足够，判断转出验证码是否正确。
                    if (token.equalsIgnoreCase(code)&& token!=null){
                        //验证码正确，判断支付密码是否正确
                        if (c_pwd.equals(card1.getC_pwd())){
                         /*   //转出转入金额变动
                    card1.setC_balance(card1.getC_balance()-transferMoney);
                    card2.setC_balance(card2.getC_balance()+transferMoney);*/

                            //支付密码正确，在业务层开启事务进行转账业务。
                            int i = accountService.transfer(cid1, cid2, transferMoney);
                            if (i>0){
                                //转账成功
                                m = new Massage<>(1, "转账成功！");
                            }else {
                                //转账失败
                                m = new Massage<>(-1, "转账失败！");
                            }
                        }else {
                            //支付密码错误
                            m = new Massage<>(-1, "支付密码错误！");
                        }
                    }else {
                        //验证码不正确
                        m = new Massage<>(-1, "验证码错误！");
                    }
                }else {
                    //余额不足
                    m = new Massage<>(-1, "余额不足，请换卡支付！");
                }
            }else {
                //未过审
                m = new Massage<>(-1, "账号未过审，请换卡支付！");
            }
        }else {
            //账号不存在
            m = new Massage<>(-1, "银行卡账号不存在，请输入正确的卡号！");
        }

        //拿到转账详情，用json返回给前端页面
        String s=JSON.toJSONString(m);
        resp.getWriter().write(s);
    }


    //提现
    //事务
    //收款账户是否过审
    //提现金额小于app余额
    //验证码是否正确
    //支付密码是否正确
    //提现是否成功
    //日志
    public void withdrawal(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String cid = req.getParameter("c_id");
        String uid = req.getParameter("u_id");
        Double outMoney=Double.parseDouble(req.getParameter("fmoney"));
        String u_cpwd = req.getParameter("apwd");//支付密码
        String code = req.getParameter("code");//验证码
        // 获取 Session 中的验证码
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        // 删除 Session 中的验证码
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        Massage<User> m=null;
        //调用业务层判断这个账户是否存在
        User user = accountService.findUserId(uid);
        if (user!=null){
            //app余额小于提现金额
            if (user.getU_balance()>=outMoney){
                //验证码是否正确
                if (token.equalsIgnoreCase(code)&& token!=null){
                    //支付密码是否正确
                    if (u_cpwd.equals(user.getU_cpwd())){
                        //调用业务层,进行提现业务
                        int i = accountService.getMoney(cid, user, outMoney);
                        if (i>0){
                            //提现成功
                            m = new Massage<>(1, "提现成功！");
                        }else {
                            //提现失败
                            m = new Massage<>(-1, "提现失败！");
                        }
                    }else {
                        //app支付密码错误
                        m = new Massage<>(-1, "app支付密码错误！");
                    }
                }else {
                    //验证码错误
                    m = new Massage<>(-1, "验证码错误！");
                }
            }else {
                //app余额不足，无法提现
                m = new Massage<>(-1, "app余额不足，无法提现！");
            }

        }else {
            //没有可用银行卡
            m = new Massage<>(-1, "无可用银行卡！");
        }
        //拿到转账详情，用json返回给前端页面
        String s=JSON.toJSONString(m);
        resp.getWriter().write(s);


    }


    //通过u_id查询这个用户
    public void findByUid(HttpServletRequest req, HttpServletResponse resp) throws Exception{
        String u_id = req.getParameter("u_id");
        User user = accountService.findUserId(u_id);
        Massage<User> m=null;
        if (user!=null){
            //成功
            m=new Massage<>(1,"成功",user);
        }else {

            m=new Massage<>(-1,"用户不存在");

        }
        String s = JSON.toJSONString(m);
        resp.getWriter().write(s);

    }











}
